package com.lagou.server;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class MyClassLoader extends ClassLoader{

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try{
            FileInputStream in = new FileInputStream(name + ".class");
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len = -1;
            while((len = in.read(buf)) != -1) {
                byteArrayOutputStream.write(buf , 0 , len);
            }
            in.close();
            byte[] classBytes = byteArrayOutputStream.toByteArray();
            return defineClass(classBytes, 0, classBytes.length);
        }catch (Exception e){
            e.printStackTrace();
        }
        return super.findClass(name + ".class");
    }
}
